package com.example.state;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.example.bounce.Bounce;
import com.example.game.Game;
import com.example.game.Spring;
import com.example.object.game.Ball;
import com.example.glsurfaceview.MyGLSurfaceView;
import com.example.glutil.WorldCoord;

import org.jbox2d.common.Vec2;

/**
 * Created by Lucas on 06/06/13.
 */
public class InGame extends AppState {

    /**
     * The debug TAG
     */
    private final String TAG = "InGame";
    /**
     * The game
     */
    private Game _game;
    /**
     * The Y point that handle the change of view
     */
    private float _yPrevious;
    /**
     * The X coordinate previously touch
     */
    private float _xPrevious;
    /**
     * true is a ball is touched
     */
    private boolean _ballTouched;
    /**
     * The touched ball
     */
    private Ball _touchedBall;
    /**
     * Mouse joint to handle the motion on the ball
     */
    private Spring _spring;

    /**
     * The constructor
     * @param context the context of the application
     * @param glView the GLSurfaceView
     */
    public InGame(Context context, MyGLSurfaceView glView) {
        super(context, glView);
        _bounce = (Bounce)context;
        _game = new Game();
        _spring = new Spring(_game.getWorld());
        _yPrevious = 0;
    }

    /**
     * Initialize object in the the list of game shape
     */
    @Override
    public void initGameShape(){
        Log.i(TAG, "Initialize game's shapes");
        _game.initializeGame(_bounce.getDifficulty(), _bounce.getWorld());
        _game.initGameShape(_listGameShape, _context);
    }

    /**
     * Calculate the nex position in the list of game shape
     */
    public void calculateNextFrame(){
        _game.updateWorld();
        for(int i=0; i<_listGameShape.size(); i++){
            if(_listGameShape.get(i).isMovable())
                _listGameShape.get(i).calculateNextCoordinate();
        }
    }

    /**
     * Handle the input touch event
     * @param e the input motion event
     * @return true if the event have been handled
     */
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        super.onTouchEvent(e);
        // MotionEvent reports input details from the touch screen
        // and other input controls. In this case, you are only
        // interested in events where the touch position changed.
        if(_currentState == INGAME){
            Vec2 touch = new Vec2(DEVICE_HEIGHT - e.getY(), e.getX());
            touch = WorldCoord.getCoordinates(_bounce, touch);

            switch (e.getAction()){
                case MotionEvent.ACTION_DOWN:
                    _xPrevious = touch.x;
                    _yPrevious = touch.y;
                    for (int i=0; i<_listGameShape.size(); i++){
                        if(_listGameShape.get(i).isTouchable()){
                            if (_listGameShape.get(i).isTouched(touch)){
                                // create a mouse join to move the touched ball
                                _touchedBall = (Ball) _listGameShape.get(i);
                                //_touchedBall.getBody().setAwake(false);
                                _ballTouched = true;
                                _spring.bind(touch, _touchedBall.getBody());
                                Log.d(TAG, "Ball is touched");
                                return false;
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (_ballTouched){
                        //_touchedBall.moveTo(touch.y, touch.x);
                        _spring.update(touch);
                        //_touchedBall.applyForce(touch);
                        // change the target of the mouse join
                    }else {
                        //Log.i(TAG, "Change of view");
                        float direction = touch.x - _xPrevious;
                        if(direction<0){
                            direction = 1.0f;
                        }else {
                            direction = -1.0f;
                        }
                        _bounce.changeView(direction);

                    }
                    return false;
                case MotionEvent.ACTION_UP:
                    if (_ballTouched){
                        //delete the mouse join !
                        Log.d(TAG, "action up");
                        _ballTouched = false;

                        _touchedBall = null;
                        _xPrevious = 0;
                        _yPrevious = 0;
                        _spring.destroy();
                    }
                    return false;
            }
        }
        return true;
    }

    /**
     * Handle the key down event to change the current state
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            _bounce.changeState(WORLD);
            return true;
        }
        return false;
    }
}
